<?php
    /**
     * Created by PhpStorm.
     * User: jackhhy
     * Date: 2019/7/5-15:13
     * Link: https://gitee.com/luckygyl/LuckyAdmin
     * FileName: AdminMember.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     *
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */

    namespace app\admin\controller;

    use app\admin\validate\Aadmimember;
    use service\TreeService;
    use service\UtilService;
    use think\Exception;
    use think\Validate;

    class Adminmember extends SystemBase
    {

        protected $admin_member_model;
        protected $admin_role;

        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub
            $this->admin_member_model = new \app\admin\model\AdminMember();//后台管理员
            $this->admin_role         = new \app\admin\model\AdminRole();//角色模型
        }


        /**
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: Jackhhy <jackhhy520@qq.com>
         * @name: index
         * @describe:  管理员列表
         */
        public function index()
        {
            if($this->request->isAjax()) {
                $param = $this->request->param();//接收参数
                /**
                 * 返回获取的分页数据
                 */
                return $this->admin_member_model->GetAdminMemberDataPage($param);
            }
            //实例化角色表
            $role = new \app\admin\model\AdminRole();
            $data = $role->GetAllJson();

            $this->assign("data", $data);
            return $this->fetch();
        }


        /**
         * @return mixed|void
         * @author: Jackhhy <jackhhy520@qq.com>
         * @name: AddMember
         * @describe:添加后台管理员
         */
        public function AddMember()
        {
            if($this->request->isAjax()) {
                $data = $this->request->post();

                try {
                    /*  //验证数据
                      $validate = new Validate([
                          ['nickname|token', 'chsAlpha', '管理员昵称只允许汉字、字母'],
                          ['username', 'alphaNum', '用户名只允许字母与数字'],
                          ['password', 'min:6|max:16|alphaDash', '密码最少6个字符|密码最长16个字符|密码只允许字母、数字和下划线 破折号'],
                      ]);*/
                    $valdata = new Aadmimember();//实例化验证器
                    //验证数据合法性
                    if(!$valdata->check($data)) {
                        $this->error('提交失败：'.$valdata->getError());
                    }

                    $data['password'] = UtilService::think_encrypt($data['password']);//密码加密,可逆加密
                    //添加数据
                    // dump($data);
                    // die();
                    $res = $this->admin_member_model->addData($data);
                    if($res!=false) {

                        $this->AddLogs('添加后台管理员');

                        $this->success("添加成功");
                    }
                    else {
                        $this->error("添加失败");
                    }

                } catch (Exception $exception) {  //捕获异常
                    $this->error($exception->getMessage());
                }
            }
            $tree = TreeService::toFormatTree($this->admin_role->GetRoleList(), "name");//获取菜单列表
            //dump($tree);die;
            $this->assign(compact("tree"));
            return $this->fetch();
        }



        /**
         * @return mixed|void
         * @author: Jackhhy <jackhhy520@qq.com>
         * @name: EditMenu
         * @describe:编辑管理员信息
         */
        public function EditMember()
        {
            if($this->request->isAjax()) {
                $data = $this->request->post();
                try {
                    $valdata = new Aadmimember();//实例化验证器
                    //验证数据合法性
                    if(!$valdata->check($data)) {
                        $this->error('提交失败：'.$valdata->getError());
                    }
                    $data['password'] = UtilService::think_encrypt($data['password']);//密码加密,可逆加密

                    $res = $this->admin_member_model->editData($data);//编辑数据

                    if($res!==false) {

                        $this->AddLogs('编辑管理员'.$data['nickname'].'信息');


                         $this->success("修改成功");
                    }
                    else {
                         $this->error("修改失败");
                    }
                } catch (Exception $exception) { //捕获异常
                     $this->error($exception->getMessage());
                }
            }
            $tree = TreeService::toFormatTree($this->admin_role->GetRoleList(), "name");//获取菜单列表

            $id      = $this->request->param("id/d");//父id
            $info    = $this->admin_member_model->get($id)->toArray();
            $role_id = $info['role_id'];

            $info['password'] = UtilService::think_decrypt($info['password']); //密码解密

            $this->assign(compact("role_id", "tree", "info"));

            return $this->fetch();
        }


        /**
         * @author: Jackhhy <jackhhy520@qq.com>
         * @name: delete
         * @describe:删除管理员
         */
        public function delete()
        {
            if($this->request->isAjax()) {
                $ids = $this->request->post("ids");
                try {
                    $res = $this->admin_member_model->del($ids); //模型删除
                    if($res) {
                        $this->AddLogs('删除管理员');

                        $this->success("删除成功");
                    }
                    else {
                        $this->error("删除失败");
                    }
                } catch (Exception $exception) {
                    $this->error($exception->getMessage());
                }
            }
        }



    }